// ml:run = $bin < B-small-attempt5.in > B-small.out
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>

using ll = long long;

int const maxn = 100007;
ll x[maxn];
ll y[maxn];
ll r[maxn];
ll s[maxn];
ll a[maxn];
ll b[maxn];
ll suma[maxn];
ll sumb[maxn];
ll n, k, a1, b1, c, d, e1, e2, f;

int main()
{
    std::ios::sync_with_stdio(false);
    int T; std::cin >> T;
    for (int ti = 1; ti <= T; ti++) {
        std::cout << "Case #" << ti << ": ";
        std::cin >> n >> k >> a1 >> b1 >> c >> d >> e1 >> e2 >> f;
        x[1] = a1; y[1] = b1;
        r[1] = s[1] = 0;
        a[1] = a1; b[1] = b1;
        for (int i = 2; i <= n; i++) {
            x[i] = (c * x[i - 1] + d * y[i - 1] + e1) % f;
            y[i] = (d * x[i - 1] + c * y[i - 1] + e2) % f;
            r[i] = (c * r[i - 1] + d * s[i - 1] + e1) % 2;
            s[i] = (d * r[i - 1] + c * s[i - 1] + e2) % 2;
        }
        for (int i = 2; i <= n; i++) {
            if (r[i]) r[i] = -1;
            else r[i] = 1;
            if (s[i]) s[i] = -1;
            else s[i] = 1;

            a[i] = r[i] * x[i];
            b[i] = s[i] * y[i];

            // std::cout << a[i] << "  " << b[i] << "\n";
        }

        suma[0] = sumb[0] = 0;
        for (int i = 1; i <= n; i++) {
            suma[i] = suma[i - 1] + a[i];
            sumb[i] = sumb[i - 1] + b[i];
        }
        std::vector<ll> ans;
        for (int i = 1; i <= n; i++)
            for (int j = i; j <= n; j++) {
                auto tsa = suma[j] - suma[i - 1];
                for (int i1 = 1; i1 <= n; i1++)
                    for (int j1 = i1; j1 <= n; j1++) {
                        auto tsb = sumb[j1] - sumb[i1 - 1];
                        ans.push_back(tsa * tsb);
                    }
            }
        std::sort(ans.begin(), ans.end(), std::greater<ll>());
        std::cout << ans[k - 1] << "\n";
    }
}

